//
//  SSShopingCartTool.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/3/5.
//

import UIKit

public protocol SSCartToolDelegate: NSObjectProtocol {
    func cartResult()
}

class SSShopingCartTool: UIView {
    weak var delegate: SSCartToolDelegate?  // 代理
    var totalLab: UILabel!     // 减按钮
    var priceLab: UILabel!     // 加按钮
    var btn: UIButton!    // 数量展示/输入框
    var totalPrice : Float = 0//总价格
    
    override public init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        //整个控件的默认尺寸(和某宝上面的按钮同样大小)
        if frame.isEmpty {self.frame = CGRect(x: 0, y: 0, width: 110, height: 30)}
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
    }
    
    override open func awakeFromNib() {
        setupUI()
        let notify = NSNotification.Name(rawValue:kShopingCartNotify)
        NotificationCenter.default.addObserver(self, selector: #selector(changeTitle), name: notify, object: nil)
    }
    
    @objc func changeTitle() {
        if  let fee = kUserDefaults.value(forKey: kShopingCartFee)
        {
            totalPrice = fee as! Float
            print("fee\(fee)")
        }
        priceLab.text = "$\(totalPrice)"
    }
    
    ///delloc方法
    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    //设置UI布局
    fileprivate func setupUI() {
        
        let totalLab = UILabel()
        totalLab.font = UIFont.systemFont(ofSize: 12)
        totalLab.text = "合计："
        totalLab.frame = CGRect(x: 16, y: 22, width: 40, height: 16)
        addSubview(totalLab)
        self.totalLab = totalLab
        let priceLab = UILabel()
        priceLab.font = UIFont.systemFont(ofSize: 18,weight: .bold)
        priceLab.text = "$0"
        priceLab.frame = CGRect(x: totalLab.frame.maxX + 2, y: 16, width: 100, height: 22)
        addSubview(priceLab)
        self.priceLab = priceLab
        
        if  let fee = kUserDefaults.value(forKey: kShopingCartFee)
        {
            totalPrice = fee as! Float
            print("fee\(fee)")
        }
        priceLab.text = "$\(totalPrice)"
        
//        backgroundColor = UIColor.white
        let button = UIButton()
        button.setTitle("查看购物车", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16,weight: .bold)
        button.backgroundColor = UIColor.themeColor
        button.setTitleColor(UIColor.init(hex: "#E5E5E5"), for: .normal)
        button.addTarget(self, action:#selector(btnTapped(_:)), for:.touchUpInside)
        button.frame = CGRect(x: ScreenWidth - 170 - 16, y: 8, width: 170, height: 48)
        button.layer.cornerRadius = 8
        button.layer.masksToBounds = true
        addSubview(button)
        self.btn = button
        

    }
    
    @objc func btnTapped(_ button:UIButton){
        delegate?.cartResult()//添加按钮
        if btn.titleLabel?.text == "查看购物车" {
            let currentVC = UIViewController.current()
            if (currentVC.presentingViewController != nil) {//模态弹出
                currentVC.dismiss(animated: true) {
                    let vc = SSUserOrderViewController.init(nibName: "SSUserOrderViewController", bundle: nil)
                    UIViewController.current().navigationController?.pushViewController(vc, animated: true)
                }
            }else{//模态push
                let vc = SSUserOrderViewController.init(nibName: "SSUserOrderViewController", bundle: nil)
                UIViewController.current().navigationController?.pushViewController(vc, animated: true)
            }
        }
    }
}
